package com.myz.rabbitmq.routing;


import com.myz.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 路由模式
 * exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT)
 *
 * @author maoyz on 18-4-4.
 */
public class RoutingSendDirect {
    private static final Logger logger = LoggerFactory.getLogger(RoutingSendDirect.class);

    private static final String EXCHANGE_NAME = "direct_logs";
    /**
     * 路由关键字
     */
    private static final String[] routingKeys = new String[]{"info", "warning", "error"};

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        // 声明交换机 direct
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        //发送信息
        for (String routingKey : routingKeys) {
            String message = "RoutingSendDirect Send the message level: " + routingKey;
            channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
            logger.debug("RoutingSendDirect Send" + routingKey + "':'" + message);
        }
        channel.close();
        connection.close();
    }
}
